<?php
/**
 * Description: 机器参数型号表
 * Author: hezhizheng
 * Date: 2018/6/29
 * Time: 14:14
 * Created by Created by Panxsoft.
 */

namespace App\Models\Panx;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;

/**
 * @property int id
 * @property string device_name
 * @property string specification
 *
 * @property-read SkinDetector skinDetector
 * @property-read Collection|Device[] devices
 *
 * @mixin Builder
 */
class DeviceSpec extends Model
{
    protected $table = 'device_specs';

    protected $guarded = [];

    const ID_DEFAULT = 1; // 默认配置 ID

    /** @var array 字段名称 和字段的默认值 */
    const DEVICE_SPEC_KEYS = [
        [
            'name'          => '睫毛上下值',
            'field_name'    => 'eyelash_threshold',
            'default_value' => '0',
            'min_value'     => '-1',
            'max_value'     => '1',
        ],
        [
            'name'          => '睫毛长短',
            'field_name'    => 'eyelash_length',
            'default_value' => '0.12',
            'min_value'     => '0',
            'max_value'     => '1',
        ],
        [
            'name'          => '口红羽化',
            'field_name'    => 'lip_feather',
            'default_value' => '0.12',
            'min_value'     => '0.1',
            'max_value'     => '2',
        ],
        [
            'name'          => '美瞳大小',
            'field_name'    => 'pupil_size',
            'default_value' => '0.23',
            'min_value'     => '0',
            'max_value'     => '1',
        ],
        [
            'name'          => '眼影亮度',
            'field_name'    => 'eyeshadow_bright',
            'default_value' => '3.5',
            'min_value'     => '1',
            'max_value'     => '20',
        ],
        [
            'name'          => '美颜程度',
            'field_name'    => 'beautifulness',
            'default_value' => '0.32',
            'min_value'     => '0',
            'max_value'     => '1',
        ],
        [
            'name'          => '美白程度',
            'field_name'    => 'whiteness',
            'default_value' => '2',
            'min_value'     => '2',
            'max_value'     => '10',
        ],
        [
            'name'          => '眉毛缩放',
            'field_name'    => 'eyebrow_height',
            'default_value' => '0.25',
            'min_value'     => '0.1',
            'max_value'     => '0.5',
        ],
        [
            'name'          => '眉毛拉伸',
            'field_name'    => 'eyebrow_width',
            'default_value' => '0.15',
            'min_value'     => '0',
            'max_value'     => '0.5',
        ],
    ];

    /**
     * @used-by $devices
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function devices()
    {
        return $this->hasMany(Device::class, 'device_spec_id', 'id');
    }

    /**
     * @return DeviceSpec
     */
    public static function default(): self
    {
        return (new self)->find(self::ID_DEFAULT);
    }

    public static function findByCode(?string $code): ?self
    {
        return (new self)
            ->where('code', $code)
            ->first();
    }
}